{include mobile_template('header')}
<div class="panel-comm panel-fullsize">
    <ul class="list-group J_address_list">
        <!--{loop $address_list $address}-->
        <li class="list-group-item J_address_item" data-id="$address['id']">
            <div>
                 <div class="fr">
                    <span class="txt-small txt-grey">Tel：<span data-name="mobile">$address['mobile']</span></span>
                </div>
                {if $address[is_default]}<span class="label-comm label-small label-primary">默认</span>{/if}   
                <span data-name="name">$address['name']</span>
            </div>
            <span class="txt-small txt-grey" data-name="addr">$address['addr']</span>
            <span class="txt-small txt-grey">邮编:<span data-name="postcode">$address['postcode']</span></span>
        </li>
        <!--{/loop}-->
    </ul>
    <div class="panel-body">
        <a href="#" class="btn btn-mycolor block J_address_btn">新增收货地址</a>
    </div>
</div>
<div class="panel-comm panel-default none" id="add_addr_box">
    <div class="panel-heading">增加/编辑收货地址</div>
    <div class="panel-body">
        <form action="{url member/mobile/do/address/op/add}">
            <div class="form-box">
                <div class="form-item">
                    <label for="">姓名<span class="txt-small">(必填)</span>：</label>
                    <input type="text" name="name">
                </div>
                <div class="form-item">
                    <label for="">地址<span class="txt-small">(必填)</span>：</label>
                    <input type="text" name="addr">
                </div>
                <div class="form-item">
                    <label for="">邮编：</label>
                    <input type="text" name="postcode">
                </div>
                <div class="form-item">
                    <label for="">手机号<span class="txt-small">(必填)</span>：</label>
                    <input type="text" name="mobile">
                </div>
                <div class="form-submit">
                    <div class="btn-group btn-group-auto">
                        <div class="btn-group">
                            <a class="btn btn-mycolor" id="submit_btn" href="javascript:" data-type="submit">提交</a>
                        </div>
                        <div class="btn-group">
                            <a class="btn btn-default" href="javascript:" data-type="close">取消</a>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
$(document).ready(function() {

    addr = new address({
        address_form:'#add_addr_box'
    });

    //事件绑定
    $('a.J_address_btn').click(function(event) {
        event.preventDefault();
        addr.add(function(result, data){
            if(result.code == 200) {
                document_reload();
            } else {
                alert(result.message);
            }
        });
    });

    $('ul.J_address_list').m_list_touch({
        item_exp:'li.J_address_item',
        tool:
        [
            {
                title:'设默认',
                click:function(group, item, event) {
                    addr.set_default(item.element.data('id'), function(result) {
                        if(result.code == 200) {
                            document_reload();
                        } else {
                            alert(result.message);
                        }
                    });
                }
            },
            {
                title:'编辑',
                click:function(group, item, event) {
                    var data = {};
                    data.id = item.element.data('id');
                    data.name = item.element.find('[data-name="name"]').text();
                    data.mobile = item.element.find('[data-name="mobile"]').text();
                    data.addr = item.element.find('[data-name="addr"]').text();
                    data.postcode = item.element.find('[data-name="postcode"]').text();
                    addr.edit(data, function(result){
                        if(result.code == 200) {
                            document_reload();
                        } else {
                            alert(result.message);
                        }
                    });
                }
            },
            {
                title:'删除',
                css_class:'tool-del',
                click:function(group, item, event) {
                    if(!confirm('您确定要删除这个地址吗？')) return;
                    addr.delete(item.element.data('id'), function(result) {
                        if(result.code==200) {
                            group.items.delete(item.element.data('index'));
                            if(group.items.length()<1) {
                                group.remove();
                            }
                        } else {
                            alert(result.message);
                        }
                    });
                }
            }
        ]
    });
});


//收货地址处理
var address = function(option) {

    var add_addr;

    var post_type = '';
    var post_callback;
    var post_url = '';

    function init() {

        //弹出层初始化
        add_addr = new $.m_drawer(option.address_form, {}, {
            onSubmit:function(container, workArea) {
                post(workArea.find('form'));
            }
        });

    }

    //提交表单
    function post(form) {
        $.post(post_url, form.serialize(), function(data, textStatus, xhr) {
            if(is_message(data)) {
                myAlert(data);
                return;
            } else if(!is_json(data)) {
                alert('返回错误，请稍候尝试。');
                return;
            }
            var json = parse_json(data);
            if(json.code!=200) {
                if(!add_callback) {
                    alert(json.message);
                    return;
                }
            } else {
                form[0].reset();
                add_addr.close();
            }
            if(post_callback) {
                post_callback(json, form);
            }
        });
    }

    //设置编辑值
    function set_from_value(form, data) {
        form.find('[name="name"]').val(data.name);
        form.find('[name="addr"]').val(data.addr);
        form.find('[name="postcode"]').val(data.postcode);
        form.find('[name="mobile"]').val(data.mobile);
        form.find('[name="id"]').remove();
        form.append('<input type="hidden" name="id" value="'+data.id+'" />');
    }

    //删除一条地址数据
    function del(id, callback) {
        var post_action = "{url member/mobile/do/address/op/delete}";
        $.post(post_action.url(), {id:id, in_ajax:1}, function(data) {
            if(is_message(data)) {
                myAlert(data);
                return;
            }
            var json = parse_json(data);
            if(!callback) {
                if(json.code != 200) {
                    alert(json.message);
                }
            } else {
                callback(json);
            }
        });
    }

    //设置一条地址为默认地址
    function set_default(id, callback) {
        var post_action = "{url member/mobile/do/address/op/set_default}";
        $.post(post_action.url(), {id:id, in_ajax:1}, function(data) {
            if(is_message(data)) {
                myAlert(data);
                return;
            }
            var json = parse_json(data);
            if(!callback) {
                if(json.code != 200) {
                    alert(json.message);
                }
            } else {
                callback(json);
            }
        });
    }

    init();

    return {
        add:function(callback) {
            post_callback = null;
            if(callback) post_callback = callback;
            post_url = Url('member/mobile/do/address/op/add').url();

            add_addr.open(function(container, workArea){
                workArea.find('form').find('[name="id"]').remove()
            });
        },
        edit:function(data, callback) {
            post_callback = null;
            if(callback) post_callback = callback;
            post_url = Url('member/mobile/do/address/op/edit').url();

            add_addr.open(function(container, workArea){
                set_from_value(workArea.find('form'), data);
            });
        },
        set_default:function(id, callback) {
            set_default(id, callback);
        },
        delete:function(id, callback) {
            del(id, callback);
        }
    }
}

</script>
{include mobile_template('footer')}